package com.dgut.cbd.finalexp.service;

import com.dgut.cbd.finalexp.dao.AdminDao;
import com.dgut.cbd.finalexp.entity.MyAdmin;
import com.dgut.cbd.finalexp.entity.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Program: finalexp
 * @Description:
 * @Author: cbd
 * @Create: 2021-06-21
 * @Version: 1.0.0
 **/
@Service
public class AdminService implements UserDetailsService {

    @Autowired
    AdminDao adminDao;

    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        System.out.println("请求认证的管理员id：" + id);

        MyAdmin admin = adminDao.loadAdminFromId(id);

        if (admin == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        return new User(id, admin.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN"));
    }
}
